package com.itheima.es;

import com.alibaba.fastjson.JSON;
import com.itheima.es.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * 演示: 操作es客户端
        分页 排序
 *
 * @作者 itcast
 * @创建日期 2021/3/19 9:09
 **/
public class EsDemo04 {
    RestHighLevelClient client;

    /**
     * note: 唱歌或javaee
     * match:
     * 年龄降序
     * @throws IOException
     */
    @Test
    public void match() throws IOException {
        int page = 2;
        int pageSize = 2;
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery("note","同学"));
        // 设置排序
        builder.sort("age", SortOrder.DESC);
        builder.sort("gender", SortOrder.ASC);
        // 分页设置
        builder.from((page-1)*pageSize);
        builder.size(pageSize);

        printResult(builder);
    }


    public void printResult(SearchSourceBuilder builder) throws IOException {
        //      * 1. 创建请求对象  SearchRequest
        SearchRequest request = new SearchRequest("user");
//      * 1.1 整个请求参数对象通过 SearchSourceBuilder 构建
        request.source(builder); // 一定要放到请求中
//      *  2. 执行对应方法  client.search
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//      *  3. 解析SearchResponse 搜索结果
        SearchHits hits = response.getHits();
        System.out.println("查询到的总数量: " + hits.getTotalHits().value);
//      *    SearchResponse
//                *      response.getHits.getTotal.value 总数量
//                *
        SearchHit[] docArray = hits.getHits();
//       *      response.getHits.getHits 得到文档数组
        for (SearchHit doc : docArray) {
            System.out.println("文档得分:" + doc.getScore());
            String userJson = doc.getSourceAsString();
            User user = JSON.parseObject(userJson, User.class);
            System.out.println(user);
        }
//       *                          遍历文档数组
//                *                               每一个元素 获取里面source字段内容
    }



    @Before
    public void init(){
        // 创建操作es的客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.200.129",9200,"http")
                )
        );
    }
    @After
    public void close(){
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        // 创建操作es的客户端
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("192.168.200.129",9200,"http")
//                )
//        );
//        // 使用client操作es
//
//        // 使用完毕后需要释放链接
//        try {
//            client.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
